# -*- coding: utf-8 -*-
# @Time    : 2021/8/23 上午12:33
# @Author  : lilong
# @desc    :


from typing import Optional

from flask_openapi3 import OpenAPI
from flask_openapi3.models import Info, Tag, Server
from flask_openapi3.models.security import HTTPBearer
from pydantic import BaseModel, Field

# *********************开始配置*******************
info = Info(title='TATlong-Bot-RASA API',
            description="Rasa 服务器提供端点来检索对话跟踪器以及端点来修改它们。 "
                        "此外，还提供了用于训练和测试模型的端点。",
            version='1.0.0'
            )

server = Server(url="http://localhost:5005",
                description="Local development server"
                )

securitySchemes = {"jwt": HTTPBearer(bearerFormat="JWT")}

app = OpenAPI(__name__, info=info, securitySchemes=securitySchemes)

book_tag = Tag(name='book', description='图书')

security = [{"jwt": []}]


class Path(BaseModel):
    bid: int = Field(..., description='图书id')


class BookData(BaseModel):
    age: Optional[int] = Field(..., ge=2, le=4, description='年龄')
    author: str = Field(None, min_length=2, max_length=4, description='作者')


class BookDataWithID(BaseModel):
    bid: int = Field(..., description='图书id')
    age: Optional[int] = Field(None, ge=2, le=4, description='年龄')
    author: str = Field(None, min_length=2, max_length=4, description='作者')


class BookResponse(BaseModel):
    code: int = Field(0, description="状态码")
    message: str = Field("ok", description="异常信息")
    data: BookDataWithID


Server_Information_tag = Tag(name='Server Information', description='图书')


@app.get('/',
         tags=[Server_Information_tag],
         responses={"200": BookResponse},
         security=security)
def get_root(path: Path, query: BookData):
    """Health endpoint of Rasa Server
    This URL can be used as an endpoint to run health checks against.
    When the server is running this will return 200.
    """
    return {"code": 0,
            "message": "ok",
            "data": {"bid": path.bid,
                     "age": query.age,
                     "author": query.author}
            }, 522


# @app.get('/book/<int:bid>', tags=[book_tag],
#          responses={"200": BookResponse},
#          security=security)
# def get_book(path: Path, query: BookData):
#     """获取图书
#     根据图书id获取图书
#     """
#     return {"code": 0,
#             "message": "ok",
#             "data": {"bid": path.bid,
#                      "age": query.age,
#                      "author": query.author}
#             }, 522
#
#
# @app.get('/book', tags=[book_tag])
# def get_books(query: BookData):
#     """获取所有书籍
#     """
#     return {
#         "code": 0,
#         "message": "ok",
#         "data": [
#             {"bid": 1, "age": query.age, "author": query.author},
#             {"bid": 2, "age": query.age, "author": query.author}
#         ]
#     }
#
#
# @app.post('/book', tags=[book_tag])
# def create_book(body: BookData):
#     """新建书籍
#     """
#     print(body)
#     return {"code": 0, "message": "ok"}
#
#
# @app.put('/book/<int:bid>', tags=[book_tag])
# def update_book(path: Path, body: BookData):
#     print(path)
#     print(body)
#     return {"code": 0, "message": "ok"}
#
#
# @app.delete('/book/<int:bid>', tags=[book_tag])
# def delete_book(path: Path):
#     print(path)
#     return {"code": 0, "message": "ok"}


if __name__ == '__main__':
    app.run(debug=True)
